package com.wangsd.web.shop.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.exception.BusinessException;
import com.wangsd.web.shop.entity.Coupon;
import com.wangsd.web.shop.entity.CouponOrder;
import com.wangsd.web.shop.entity.Merchant;
import com.wangsd.web.shop.service.CouponOrderService;
import com.wangsd.web.shop.service.CouponService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "代金券")
@RestController
@RequestMapping("/shop/coupon")
@Slf4j
public class CouponController extends MyController {

    @Autowired
    private CouponService service;
    @Autowired
    private CouponOrderService couponOrderService;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             @ApiParam(value = "排序字段") @RequestParam(required = false) String sortField,
                             @ApiParam(value = "排序方式") @RequestParam(required = false) String sortOrder,
                             Coupon queryObj) {
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Coupon> list = service.selectPage(queryObj, loginUser.getParentCode(), sortField, sortOrder);
        return Result.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "创建代金券")
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody Coupon entity) {
        if (entity.getOnedayQuota() == null) {
            entity.setOnedayQuota(0);
        }
        service.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "创建商家券")
    @PostMapping("/saveOrUpdate2")
    public Object saveOrUpdate2(@RequestBody Coupon entity) {
//        log.debug(JSON.toJSONString(entity));
//        if (entity.getValidTime() != null) {
//            String beginTime = DateUtils.formatDate(DateUtils.parseDate(entity.getValidTime().get(0), "yyyy-MM-dd'T'HH:mm:ss"));
//            String endTime = DateUtils.formatDate(DateUtils.parseDate(entity.getValidTime().get(1), "yyyy-MM-dd'T'HH:mm:ss"));
//            entity.setAvailableBeginTime(beginTime);
//            entity.setAvailableEndTime(endTime);
//        }
        if (entity.getOnedayQuota() == null) {
            entity.setOnedayQuota(0);
        }
        entity.setAvailableBeginTime(entity.getAvailableBeginTime() + " 00:00:01");
        entity.setAvailableEndTime(entity.getAvailableEndTime() + " 23:59:59");
        service.saveOrUpdate2(entity);
        return Result.success();
    }

    @ApiOperation(value = "逻辑删除优惠券")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "券id", required = true) @PathVariable Long id) {
        service.deleteById(id);
        return Result.success();
    }

    @ApiOperation(value = "上架下架")
    @GetMapping("/{id}/updateStatus")
    public Result updateStatus(@ApiParam(value = "客户(商圈)id", required = true) @PathVariable Long id) {
        service.updateStatus(id);
        return Result.success();
    }

    @ApiOperation(value = "查询所有优惠券")
    @GetMapping("/selectAllByParentCode")
    public Result selectAllByParentCode(String parentCode, Integer activityType) {
        String stockType = null;
        Integer type = null;
        if (activityType == 2) {
            stockType = "NORMAL";
        }
        if (activityType == 1) {
            type = 3;
        }
        if (activityType == 3 || activityType == 4) {
            type = 2;
        }
        List<Coupon> list = service.selectAllByParentCode(parentCode, null, stockType, type);
        return Result.success(list);
    }

    @ApiOperation(value = "查询商品")
    @PostMapping("/selectAllCoupon")
    public Object selectAllCoupon(String merchantCode) {
        SysUser loginUser = getUserInfo();
        List<Coupon> list = service.selectAllCoupon(merchantCode, loginUser.getParentCode());
        return Result.success(list);
    }

    /**
     * 核销商家券
     * @param couponCode
     * @return
     */
    @GetMapping("/useMerCoupon")
    public Result useMerCoupon(String couponCode) {
        service.useMerCoupon(couponCode, null);
        return Result.success("核销成功");
    }
}
